module ziyi{
    //import $=require("jquery");
    export class Scene extends Basicui{
        private pot_list:eui.List;
        private startBtn:eui.Image;//开始按钮
        private playagainBtn:eui.Group;//再来一局按钮
        private successInfo:eui.Group;//成功提示
        private failInfo:eui.Group;//失败提示
        private findPath:FindPath;
        private catRow:number=4;
        private catCol:number=4;
        private tmpcat:Cats;
        private cat:egret.MovieClip;
        private flag:boolean=true;//猫的状态
        private successText:eui.Label;
        private timesInfo:eui.Label;
        private steps:number=0;
        private scores:number=0;
        private back:eui.Button;
        public constructor(cd?: any){
            super();
        }
        protected pre_init(): void {
            this.initPotsList();
            this.initScene();
            this.addStartBtnListener();
            this.addPlayagainBtnListener();
            this.back.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
                location.href="http://192.168.1.64/TPPro/index.php/Admin/index/home";
            },this);
        }
        private img={
            ji:"43",
            ou:"10"
        }
        private collection:eui.ArrayCollection;
        private initPotsList():void{
            this.tmpcat=new Cats();
            this.collection = new eui.ArrayCollection();
            for(let i = 0; i < 9; i ++)
            {
                if(i%2==0){
                    for(let j=0;j<9;j++){
                        this.collection.addItem({"source":"pot1_png","l":this.img['ou']});
                    }
                }else{
                    for(let j=0;j<9;j++){
                        this.collection.addItem({"source":"pot1_png","l":this.img['ji']});
                    }
                }
                
            }
            this.pot_list.dataProvider = this.collection;
            //this.pot_list.allowMultipleSelection=true;
        }
        private initScene():void{
            this.pot_list.touchEnabled=false;
            this.pot_list.touchChildren=false;
            this.cat=this.tmpcat.getStayCat();
            this.cat.visible=false;
            this.addChild(this.cat);
            this.pot_list.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
                if(this.collection.getItemAt(this.pot_list.selectedIndex).source=="pot1_png"){
                    //console.log(this.pot_list.selectedIndex);
                    this.steps++;
                    var item=this.collection.getItemAt(this.pot_list.selectedIndex);
                    var l=item.l;
                    var s="pot2_png";
                    this.collection.replaceItemAt({"source":s,"l":l},this.pot_list.selectedIndex);

                    var y:number=this.pot_list.selectedIndex%9;
                    var x:number=(this.pot_list.selectedIndex-y)/9;
                    //console.log(x+","+y);
                    //this.findPath.setObs(i,j);
                    this.findPath.setRowCol(this.catRow,this.catCol);
                    this.findPath.setObs(x,y);
                    const tmpState=this.findPath.moveState();
                    this.flag=this.findPath.getFlag();
                    if(this.flag){
                        this.cat.visible=false;
                        this.cat=this.tmpcat.getCatchedCat();
                        this.addChild(this.cat);
                        this.cat.visible=true;
                        this.flag=true;
                    }
                    if(tmpState==-1){//状态-1代表没抓住
                        console.clear();
                        this.playtimes=-1;
                        this.playagainBtn.visible=true;
                        this.failInfo.visible=true;
                        this.cat.visible=false;
                        this.pot_list.touchEnabled=false;
                        this.pot_list.touchChildren=false;
                        this.scores=10*this.steps;
                        this.uploadScores();
                    }else if(tmpState==0 ){//状态为0代表成功围住
                        console.clear();
                        this.playtimes=-1;
                        if(1000-18*this.steps>=0){
                            this.scores=1000-10*this.steps;
                        }
                        this.successText.text="恭喜你抓住了神经猫\n一共用了"+this.steps+"步\n"+"得分:"+this.scores+"分";
                        this.cat.visible=false;
                        this.playagainBtn.visible=true;
                        this.successInfo.visible=true;
                        this.pot_list.touchEnabled=false;
                        this.pot_list.touchChildren=false;
                        //this.removeChild(this.cat);
                        this.uploadScores();
                    }else{
                        if(tmpState==1){//上
                            this.catRow-=1;
                        }else if(tmpState==2){//下
                            this.catRow+=1;
                        }else if(tmpState==3){//左
                            this.catCol-=1;
                        }else if(tmpState==4){//右
                            this.catCol+=1;
                        }else if(tmpState==5){
                            if(this.catRow%2==0){
                                //奇数行左上
                                this.catRow-=1;
                                this.catCol-=1;
                            }else{
                                //偶数行右上
                                this.catRow-=1;
                                this.catCol+=1;
                            }
                        }else if(tmpState==6){
                            if(this.catRow%2==0){
                                //奇数行左下
                                this.catRow+=1;
                                this.catCol-=1;
                            }else{
                                //偶数行右下
                                this.catRow+=1;
                                this.catCol+=1;
                            }
                        }
                    }
                    if(this.catRow%2==0){
                        this.cat.x=50+10+this.catCol*66+30;
                        this.cat.y=560+55*this.catRow-25;
                    }else{
                        this.cat.x=50+43+this.catCol*66+30;
                        this.cat.y=560+55*this.catRow-25;
                    }
                }
            },this);
            this.startBtn.visible=true;
            this.playagainBtn.visible=false;
            this.successInfo.visible=false;
            this.failInfo.visible=false;
        }
        private addStartBtnListener():void{
            this.startBtn.once(egret.TouchEvent.TOUCH_TAP,()=>{
                this.startGame();
            },this);
        }
        private addPlayagainBtnListener():void{
            this.playagainBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.haddlePlayagainBtn,this);
        }
        private haddlePlayagainBtn():void{
            this.startGame();
        }
        private init():void{
            if(this.flag){
                this.cat=this.tmpcat.getStayCat();
                this.addChild(this.cat);
                this.cat.visible=true;
            }
            this.scores=0;
            this.steps=0;
            this.cat.visible=true;
            this.pot_list.touchEnabled=true;
            this.pot_list.touchChildren=true;
            this.flag=false;
            this.catRow=4;
            this.catCol=4;
            this.cat.x=50+10+this.catCol*66+30;
            this.cat.y=560+55*this.catRow-25;
            this.findPath=new FindPath();
            for(let i=0;i<9;i++){
                for(let j=0;j<9;j++){
                    if(this.findPath.ifObs(i,j)){
                        if(i%2==0){
                            var item={
                                "source":"pot2_png",
                                l:this.img['ou']
                            };
                        }else{
                            var item={
                                "source":"pot2_png",
                                l:this.img['ji']
                            };
                        }
                        this.collection.replaceItemAt(item,i*9+j);
                    }else if(this.collection.getItemAt(i*9+j).source=="pot2_png"){
                        var tmp=this.collection.getItemAt(i*9+j);
                        var source="pot1_png";
                        var l=tmp.l;
                        this.collection.replaceItemAt({"source":source,"l":l},i*9+j);
                    }
                }
            } 
        }
        private uploadScores():void{
            const request=new egret.HttpRequest();
            //request.withCredentials=true;
            request.responseType = egret.HttpResponseType.TEXT;
            //设置为 POST 请求
            request.open("http://192.168.1.64/Tppro/index.php/Admin/Demo/uploadScores/"+this.scores, egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.send();
            request.addEventListener(egret.Event.COMPLETE, this.onGetComplete1, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onGetIOError, this);
            request.addEventListener(egret.ProgressEvent.PROGRESS, this.onGetProgress, this);
        }
        private playtimes:number=-1;
        private startGame():void{
            const request=new egret.HttpRequest();
            //request.withCredentials=true;
            request.responseType = egret.HttpResponseType.TEXT;
            //设置为 POST 请求
            request.open("http://192.168.1.64/Tppro/index.php/Admin/Demo/retrieveStrength", egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.send();
            request.addEventListener(egret.Event.COMPLETE, this.onGetComplete, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onGetIOError, this);
            request.addEventListener(egret.ProgressEvent.PROGRESS, this.onGetProgress, this);
        }
        private onGetComplete(event: egret.Event): void{
            const request = <egret.HttpRequest>event.currentTarget;
            console.log(JSON.parse(request.response));
            var data=JSON.parse(request.response);
            this.playtimes=Number(data['playtimes']);
            this.timesInfo.text=data['data'];
            
            this.startBtn.visible=false;
            this.playagainBtn.visible=false;
            this.successInfo.visible=false;
            this.failInfo.visible=false;
            if(this.playtimes>0){
                if(this.startBtn.visible){
                    this.init();
                }else{
                    this.init();
                }
            }else{
                this.successInfo.visible=true;
                this.successText.visible=true;
                this.successText.text="游戏次数不足，请稍后再来......";
            }
        }   
        private onGetComplete1(event: egret.Event): void{
            const request = <egret.HttpRequest>event.currentTarget;
            console.log(JSON.parse(request.response));
        }  
        private onGetIOError(event: egret.IOErrorEvent): void {
            console.log("get error : " + event);
        }
        private onGetProgress(event: egret.ProgressEvent): void {
            console.log("get progress : " + Math.floor(100 * event.bytesLoaded / event.bytesTotal) + "%");
        }
        
    }
}